﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Proje
{
    class RouteFileConnector
    {
        FileStream file;
        StreamWriter writer;
        StreamReader reader;


        public List<Route> searchRoute(string startPoint,string endPoint,string date) {
            List<Route> liste = new List<Route>();
            string[] files = Directory.GetFiles(FilePath);
            
            try
            {
                for (int i = 0; i < files.Length; i++)
                {
                    
                    file = new FileStream(files[i], FileMode.Open);
                    reader = new StreamReader(file);
                    string firstLine = reader.ReadLine();
                    if (date.Equals(firstLine.Split('|')[0]))
                    {
                        string secondline = reader.ReadLine();
                        if (secondline.Contains(startPoint) && secondline.Contains(endPoint))
                        {
                            Route route = new Route();
                            route.bus = firstLine.Split('|')[1];
                            route.date = firstLine.Split('|')[0];
                            string[] temp = files[i].Split('\\');
                            route.number = Convert.ToInt32(temp[temp.Length -1 ]);
                            string[] items = secondline.Split('&');
                            for (int j = 0; j < items.Length; j++)
                            {
                                string stoppointstring=items[i];
                                StopPoint stopPoint = new StopPoint();
                                stopPoint.name = items[j].Split('|')[0];
                                stopPoint.time = items[j].Split('|')[1];
                                route.stopPoints.Add(stopPoint);
                            }
                            liste.Add(route);

                        }
                        
                    }
                    reader.Close();
                    file.Close();
                }
                return liste;

            }
            catch (IOException e)
            {
                return null;
            }
        
        
        }

        public bool insert(Route route) {
            try
            {
                file = new FileStream(FilePath + "\\" + route.number, FileMode.Append);
                writer = new StreamWriter(file);
                writer.WriteLine(route.date + "|" + route.bus);
                for (int i = 0; i < route.stopPoints.Count; i++)
                {

                    string s = route.stopPoints[i].name + "|" + route.stopPoints[i].time;
                    if (i != route.stopPoints.Count - 1)
                        s = s + "&";
                    writer.Write(s);
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
            finally {
                writer.Close();
                file.Close();
            }
            
        }


        private static string SettingsFolder
        {
            get
            {
                string folder = @"C:\Users\Serhat\Desktop\Proje\";
                if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
                return folder;
            }

        }

 
        string FilePath {
            get {
                return Path.Combine(SettingsFolder, "Sefer");

            }
        }
    }
}
